package ToolPack;

import MaterialPack.Fluid;

/**
 * Created by Aaron on 8/10/2014.
 */

public class FluidContainer extends Container {
    protected Class<? extends Fluid> fluidType;
    protected float fluidCapacity;
    protected float fluid;
    protected float transferRate;

    public FluidContainer (float fluidCapacity, float transferRate) {
        this(fluidCapacity, transferRate, null, 0.0f);
    }

    public FluidContainer (float fluidCapacity, float transferRate, Class<? extends Fluid> fluidType, float fluid) {
        super();
        this.fluidCapacity = fluidCapacity;
        this.transferRate = transferRate;
        this.fluidType = fluidType;
        this.fluid = fluid;
    }

    // Returns how much fluid was refilled.
    public final float refillFluid (FluidContainer source) {
        float result = 0.0f;
        if (this.fluidType == null || this.fluidType.equals(source.getFluidType())) {
            result = addFluid(source.removeFluid(transferRate));
        }
        return result;
    }

    // Returns how much fluid was added.
    // Add the minimum of fluidToAdd, transferRate, and fluidCapacity - fluid.
    public final float addFluid (float fluidToAdd) {
        float result = Math.min(fluidToAdd, this.transferRate);
        result = Math.min(result, this.fluidCapacity - this.fluid);
        this.fluid += result;
        return result;
    }

    // Returns how much fluid was removed.
    // Remove the minimum of fluidToRemove, transferRate, and fluid.
    public final float removeFluid (float fluidToRemove) {
        float result = Math.min(fluidToRemove, this.transferRate);
        result = Math.min(result, this.fluid);
        this.fluid -= result;
        return result;
    }

    public float getFluid ( ) {
        return this.fluid;
    }

    public float getFluidCapacity ( ) {
        return this.fluidCapacity;
    }

    public Class<? extends Fluid> getFluidType ( ) {
        return this.fluidType;
    }

}
